package com.cat.testviews;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements
		MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
	static final String TEG = "testviews";

	private int currentPlaySong;
	
	//there status indicates where there progressBar's cursor
	private int progressStatus;
	private Boolean onPlayingMusic;
	private File musicFiles[];
	private MediaPlayer mediaPlayer = null;
	private ProgressBar progressBar = null;
	private Handler mHandler = new Handler();
	private ProgressThread progressThread = null;
	private Boolean stopThread = true;

	private int durationTime;
	private int currentTime;

	private class ProgressThread extends Thread {
		public ProgressThread() {
			super();
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			durationTime = mediaPlayer.getDuration();
			int progressMax = progressBar.getMax();
			while (progressStatus <= progressMax && !stopThread) {
				mHandler.post(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						progressBar.setProgress(progressStatus);
					}
				});
				currentTime = mediaPlayer.getCurrentPosition();
				progressStatus = currentTime * progressMax / durationTime;

			}
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		File musicFile = getMusicDirectory();
		musicFiles = musicFile.listFiles();
		progressStatus = 0;
		currentPlaySong = 0;
		onPlayingMusic = false;

		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
	}

	private File getMusicDirectory() {
		File file = new File(
				android.os.Environment.getExternalStorageDirectory(), "Music");
		return file;
	}

	public void play(View view) {
		// start play music
		Button playButton = (Button) view;
		if (onPlayingMusic) {
			playButton.setText(R.string.play);
			pauseMusic();
		} else {
			playButton.setText(R.string.stop);
			playMusic();
		}
	}
// 尝试比较内容
	public void next(View view) {
		
		//if there no player,need to create one here;
		//we need to reset the player here anyhow;
		if (mediaPlayer == null)
			getNewMediaPlayer();
		mediaPlayer.reset();
		
		currentPlaySong++;
		if (currentPlaySong >= musicFiles.length)
			currentPlaySong = 0;
		progressStatus = 0;
		
		setPlayerWihtCurrentSong();
		mediaPlayer.prepareAsync();
	}
	
	private void setPlayerWihtCurrentSong() {
		if (mediaPlayer == null)
			getNewMediaPlayer();
		mediaPlayer.reset();
		Uri currentPlayUri = Uri.fromFile(musicFiles[currentPlaySong]);
		try {
			mediaPlayer.setDataSource(getApplicationContext(), currentPlayUri);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void getNewMediaPlayer() {
		//create a new mediaPlayer
		
		if (mediaPlayer == null) {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setOnPreparedListener(this);
			mediaPlayer.setOnCompletionListener(this);
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		}
	}

	private void playMusic() {

		if (mediaPlayer == null) {
			getNewMediaPlayer();
			setPlayerWihtCurrentSong();
			mediaPlayer.prepareAsync();
		} else {
			
			mediaPlayer.start();
			onPlayingMusic = true;
			stopThread = false;
			progressThread = new ProgressThread();
			progressThread.start();
		}
		
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub
		mediaPlayer.start();
		onPlayingMusic = true;
		stopThread = false;
		progressThread = new ProgressThread();
		progressThread.start();
	}

	private void pauseMusic() {
		stopThread = true;
		mediaPlayer.pause();
		onPlayingMusic = false;
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		// TODO Auto-generated method stub

		currentPlaySong++;
		if (currentPlaySong >= musicFiles.length)
			currentPlaySong = 0;
		progressStatus = 0;

		if (mediaPlayer == null)
			getNewMediaPlayer();
		mediaPlayer.reset();
		setPlayerWihtCurrentSong();
		mediaPlayer.prepareAsync();
		
	}
}
